#ifndef ADMEDIACONTEXT_H
#define ADMEDIACONTEXT_H

#include "addatatype.h"
#include <QSharedPointer>
#include <QHash>
#include <QMap>
#include <QSet>

struct SimpleInfo{
    int media_id;
    int proj_id;
    QString media_cn;
    QString media_name;
    QString attr;
    int floor;
    QString area;

    SimpleInfo(){media_id=-1, proj_id = -1, floor=-10000;}

    SimpleInfo(int media_id, int proj_id, const QString &media_cn, const QString &media_name, const QString &attr, int floor, const QString &area){
        this->media_id = media_id;
        this->proj_id = proj_id;
        this->media_cn = media_cn;
        this->media_name = media_name;
        this->attr = attr;
        this->floor = floor;
        this->area = area;
    }

    SimpleInfo(const SimpleInfo &A){
        if(this != &A){
            this->media_id = A.media_id;
            this->proj_id = A.proj_id;
            this->media_cn = A.media_cn;
            this->media_name = A.media_name;
            this->attr = A.attr;
            this->floor = A.floor;
            this->area = A.area;
        }
    }

    bool operator ==(const SimpleInfo &A){
        if(this == &A) return true;
        return (    media_id==A.media_id
                    && proj_id == A.proj_id
                    && media_cn == A.media_cn
                    && media_name == A.media_name
                    && attr == A.attr
                    && floor == A.floor
                    && area == A.area
                );
    }

};

class AdMediaContext
{
public:
    AdMediaContext();
    ~AdMediaContext();
    static QSharedPointer<AdMediaContext> GetInstance();

    //注册单个设备，单个属性
    void MediaRegistry(int media_id, int proj_id, const QString &media_cn, const QString &media_name, const QString &attr, int floor, const QString &area);

    //注册单个设备，多个属性
    void MediasRegistry(int media_id, int proj_id, const QString &media_cn, const QString &media_name, const QString &attrs, int floor, const QString &area);

    //注册多个/单个模式
    void ScenesRegistry(const QString &scenes, int media_id, int proj_id, int floor, const QString &area);

    //清空所有注册设备
    void ClearAllMedias();

    //清空单个设备
    void ClearMedia(int media_id, int proj_id, const QString &media_cn, const QString &media_name, const QString &attr, int floor, const QString &area);

    //获取注册设备
    QList<SimpleInfo> GetAllRegistriedMedias(const QString &media_cn, const QString &media_name, const QString &attr, int floor, const QString &area);

    //单个筛选, status :1.解析失败, 3.确定名称+只匹配到大类  4未定名称匹配 + 非场景匹配结果, 5.未定名称匹配 + 场景匹配结果, 6.名称确定匹配结果, 数字高则优先级高
    QList<ADDataType> FilterCommandS(const ADDataType &data, int &status);

    //匹配
    QList<ADDataType> FilterCommandS(QList<ADDataType> &datas, int &prior, int floor=-10000, QString area="");


private:
    static QSharedPointer<AdMediaContext> _instance;

    QHash<QString, int> _hash_map;
    QList<SimpleInfo *> _contains;
    QList<QHash<QString, QList<SimpleInfo *> > > _medias;
    QMap<QString, QList<QString> > _name_map;
    QSet<QString> _scene_keys;
    bool IsRegistied(SimpleInfo * const &data );
    void scene_filter(QList<ADDataType> &datas);

    //解决simpleinfo被多次索引到,导致结果匹配出两次及以上的问题
    void drop_duplicates(QList<SimpleInfo> &datas);

};

#endif // ADMEDIACONTEXT_H
